package plutuscat.shishicai.domain;

import java.text.ParseException;
import java.util.Comparator;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
*
* @author dw_chenqi
* @date 2016年5月3日下午12:58:59
*/
public class Lottery {

	private Long id; // 自增id

	private String lotteryTime;//哪一期,如20160501-100

	private String dt;//日期,如2016-05-01

	private int times;//某日的第几期,一天开奖多次

	private String winNumbers;//中奖号码

	private long totalTimes;//第多少其,从第一次开彩开始计算 

	//万千百十个位数字
	private int w;
	private int q;
	private int b;
	private int s;
	private int g;

	//个位数字大小
	private DaXiao daxiao;
	//个位数字单双
	private Danshuang danshuang;
	private String addTime = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");//记录添加时间


	public static class Builder {
		private String lotteryTime;// 开奖期次,格式为日期-期次,例如:20150501-100
		private String winNumbers;

		public Builder lotteryTime(String lotteryTime) {
			this.lotteryTime = lotteryTime;
			return this;
		}

		public Builder winNumbers(String winNumbers) {
			this.winNumbers = winNumbers;
			return this;
		}

		public Lottery build() {
			try {
				Lottery lottery = new Lottery();
				lottery.setLotteryTime(lotteryTime);
				String[] lts = lotteryTime.split("-");
				String date = DateFormatUtils.format(DateUtils.parseDate(lts[0].trim(), "yyyyMMdd"), "yyyy-MM-dd");
				lottery.setDt(date);
				lottery.setTimes(Integer.parseInt(lts[1] + StringUtils.EMPTY));
				lottery.setWinNumbers(this.winNumbers);
				lottery.setW(Integer.parseInt(this.winNumbers.charAt(0) + StringUtils.EMPTY));
				lottery.setQ(Integer.parseInt(this.winNumbers.charAt(1) + StringUtils.EMPTY));
				lottery.setB(Integer.parseInt(this.winNumbers.charAt(2) + StringUtils.EMPTY));
				lottery.setS(Integer.parseInt(this.winNumbers.charAt(3) + StringUtils.EMPTY));
				int g = Integer.parseInt(this.winNumbers.charAt(4) + StringUtils.EMPTY);
				lottery.setG(g);
				lottery.setDanshuang(Danshuang.from(g));
				lottery.setDaxiao(DaXiao.from(g));
				return lottery;
			} catch (ParseException e) {
				throw new RuntimeException(e.getMessage(), e);
			}

		}
	}

	public static class LotteryComparator implements Comparator<Lottery> {

		@Override
		public int compare(Lottery o1, Lottery o2) {
			return o1.getLotteryTime().compareTo(o2.getLotteryTime());
		}

	}
	
	/**
	 * 单双
	 */
	public static enum Danshuang {
		odd("D"), even("S");
		private String s;

		private Danshuang(String s) {
			this.s = s;
		}

		public String value() {
			return this.s;
		}

		public static Danshuang from(int num) {
			return num % 2 == 0 ? Danshuang.even : Danshuang.odd;
		}
	}

	/**
	 * 大小
	 */
	public static enum DaXiao {
		big("D"), small("X");
		private String s;

		private DaXiao(String s) {
			this.s = s;
		}

		public String value() {
			return this.s;
		}

		public static DaXiao from(int num) {
			return num > 4 ? DaXiao.big : DaXiao.small;
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getDt() {
		return dt;
	}

	public void setDt(String dt) {
		this.dt = dt;
	}

	public Danshuang getDanshuang() {
		return danshuang;
	}

	public void setDanshuang(Danshuang danshuang) {
		this.danshuang = danshuang;
	}

	public DaXiao getDaxiao() {
		return daxiao;
	}

	public void setDaxiao(DaXiao daxiao) {
		this.daxiao = daxiao;
	}

	public String getLotteryTime() {
		return lotteryTime;
	}

	public void setLotteryTime(String lotteryTime) {
		this.lotteryTime = lotteryTime;
	}

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}

	public String getWinNumbers() {
		return winNumbers;
	}

	public void setWinNumbers(String winNumbers) {
		this.winNumbers = winNumbers;
	}

	public long getTotalTimes() {
		return totalTimes;
	}

	public void setTotalTimes(long totalTimes) {
		this.totalTimes = totalTimes;
	}

	public int getW() {
		return w;
	}

	public void setW(int w) {
		this.w = w;
	}

	public int getQ() {
		return q;
	}

	public void setQ(int q) {
		this.q = q;
	}

	public int getB() {
		return b;
	}

	public void setB(int b) {
		this.b = b;
	}

	public int getS() {
		return s;
	}

	public void setS(int s) {
		this.s = s;
	}

	public int getG() {
		return g;
	}

	public void setG(int g) {
		this.g = g;
	}

	public void setAddTime(String addTime) {
		this.addTime = addTime;
	}

	public String getAddTime() {
		return addTime;
	}

}
